/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.acq.service.userarea;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.acq.entity.userarea.AcqUserArea;
import com.thinkgem.jeesite.modules.acq.dao.userarea.AcqUserAreaDao;

/**
 * 区域录入员管理Service
 * @author lj
 * @version 2018-05-01
 */
@Service
@Transactional(readOnly = true)
public class AcqUserAreaService extends CrudService<AcqUserAreaDao, AcqUserArea> {

	public AcqUserArea get(String id) {
		return super.get(id);
	}
	
	public List<AcqUserArea> findList(AcqUserArea acqUserArea) {
		return super.findList(acqUserArea);
	}
	
	public Page<AcqUserArea> findPage(Page<AcqUserArea> page, AcqUserArea acqUserArea) {
		return super.findPage(page, acqUserArea);
	}
	
	@Transactional(readOnly = false)
	public void save(AcqUserArea acqUserArea) {
		super.save(acqUserArea);
	}
	
	@Transactional(readOnly = false)
	public void delete(AcqUserArea acqUserArea) {
		super.delete(acqUserArea);
	}
	@Transactional(readOnly = false)
	public Map<String,Object> getDutyArea(AcqUserArea acqUserArea) {
		Map<String,Object> result = new HashMap<String,Object>();
		try{
			List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
			//区域codes
			List<Map<String,Object>> codes =dao.getDutyAreaCodes(acqUserArea);
			for(int i=0;i<codes.size();i++){
				String str = (String)codes.get(i).get("parentIds");
				list.add(strToMap(str,(String)codes.get(i).get("code"),(String)codes.get(i).get("name")));
			}
			result.put("data",list);
			
			result.put("success", true);
			return result;
		}catch(Exception e){
			e.printStackTrace();
			result.put("status", false);
			return result;
		}
		
	}
	public Map<String,Object> strToMap(String ids,String code,String name){
		Map<String,Object> result = new HashMap<String,Object>();
		String n = "";
		String c = "";
		String[] arr = ids.split(",");
		for(String s:arr){
			if(!"0".equals(s)){
			Map<String,Object> temp = dao.getAreaById(s);	
			if(!"0".equals((String)temp.get("parentId"))){
				Map<String,Object> tm = new HashMap<String,Object>();
				n = n==""?(String)temp.get("name"):n+","+(String)temp.get("name");
				c = c==""?(String)temp.get("code"): c+ ","+(String)temp.get("code");
			
			}
			}
		}
		result.put("text", (n = n+","+name).replaceAll(",", " "));
		result.put("value", c = c+","+code);
		
		return result;
	}

	
}